<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业实现低成本数字化转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\system;

use app\common\dao\system\SystemPactDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\property\users\PropertyUsersLogRepository;
use think\exception\ValidateException;
use think\facade\Db;

class SystemPactRepository extends BaseRepository
{
    const PACT_TYPE = [
        1 => '用户协议',
        2 => '隐私政策',
        3 => '注销协议',
        5 => '支付协议',
        6 => '发布协议',
    ];

    public function __construct(SystemPactDao $dao)
    {
        $this->dao = $dao;
    }

    public function getPropertyList(array $where, $page, $limit, $propertyId = 0)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->field('id,pact_type,content,add_time,edit_time')
            ->select();
        return compact('list', 'count');
    }

    /**
     * 获取协议信息
     *
     * @param int $propertyId 企业ID
     * @param int $agreementType 协议类型
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPactInfo(int $propertyId, int $pactType)
    {
        if (!in_array($pactType, array_keys(self::PACT_TYPE))) {
            throw new ValidateException('类型错误');
        }
        $info = $this->dao->getSearch([])->where([
            'property_id' => $propertyId,
            'pact_type' => $pactType
        ])->find();

        return $info;
    }


    public function getPactType()
    {
        return self::PACT_TYPE;
    }


    public function addInfo(array $data, int $propertyId = 1, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $propertyId, $userId) {
            $data['property_id'] = $propertyId;
            $info = $this->dao->create($data);

            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($propertyId, $userId, 'SYSTEM.PACT.ADD', $data);
            return $info;
        });
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {
            $res = $this->dao->update($info['id'], $data);

            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'SYSTEM.PACT.EDIT', $data);
            return $res;
        });
    }

    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }


    public function deleteInfo($info, int $userId = 0)
    {
        /** @var PropertyUsersLogRepository $repository */
        $repository = app()->make(PropertyUsersLogRepository::class);
        $repository->addInfo($info['property_id'], $userId, 'SYSTEM.PACT.DEL', $info);
        $this->dao->delete($info['id']);
    }
}